'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Category extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // 多对多关联到视频
      this.belongsToMany(models.Video, {
        through: 'VideoCategories',
        as: 'videos',
        foreignKey: 'category_id',
        otherKey: 'video_id'
      });
      
      // 自关联，用于分类层级
      this.belongsTo(this, {
        as: 'parent',
        foreignKey: 'parent_id'
      });
      
      this.hasMany(this, {
        as: 'children',
        foreignKey: 'parent_id'
      });
    }
  }
  Category.init({
    name: DataTypes.STRING,
    description: DataTypes.TEXT,
    parent_id: DataTypes.INTEGER,
    status: DataTypes.INTEGER
  }, {
    sequelize,
    modelName: 'Category',
  });
  return Category;
};